/**
 * The review view.
 *
 * (c) Copyright 2011 Nokia Corporation. All rights reserved.
 */

import QtQuick 1.1
import com.nokia.symbian 1.1

import "../script/common.js" as CommonScript

Page {
    id: reviewView;

    anchors {
        fill: parent;
        leftMargin: 10;
        rightMargin: 10;
    }

    tools: ToolBarLayout {
         ToolButton {
             iconSource: "toolbar-back";
             onClicked: {
                 busyIndicator.opacity = 0;
                 busyIndicator.running = false;
                 reviewView.pageStack.pop();
             }
         }
    }

    Text {
        id: noReviewsInfo;
        anchors.top: parent.top;
        width: parent.width;
        text: "No reviews found for this application.";
        opacity: 0;
        wrapMode: "WordWrap";
    }

    ScrollDecorator {
        flickableItem: betaReviews;
    }

    ListView {
        id: betaReviews;
        anchors.fill: parent;
        model: betaReviewModel;
        delegate: betaReviewItem;
        spacing: 0;
        snapMode: ListView.SnapToItem;
        // The TopBar component is used as a header, so it will scroll nicely
        // along with the list content.
        header: TopBar {
            width: parent.width;
            height: 70;
            titleImage: "qrc:/gfx/title_reviews.png";
        }
    }

    XmlListModel {
        id: betaReviewModel;
        query: "/rss/channel/item";
        onStatusChanged: {
            if (status == XmlListModel.Loading) {
                busyIndicator.running = true;
                busyIndicator.opacity = 1;
            } else {
                busyIndicator.opacity = 0;
                busyIndicator.running = false;
                // If there are no reviews, tell it to the user.
                if (betaReviews.count == 0) {
                    noReviewsInfo.opacity = 1;
                } else {
                    noReviewsInfo.opacity = 0;
                }
            }
        }

        XmlRole {
            name: "title";
            query: "title/string()";
        }

        XmlRole {
            name: "description";
            query: "description/string()";
        }

        XmlRole {
            name: "category";
            query: "category/string()";
        }

        XmlRole {
            name: "guid";
            query: "guid/string()";
        }
    }

    Component {
        id: betaReviewItem;

        Item {
            width: parent.width;
            height: reviewTitle.height + 20;

            Row {
                width: parent.width;
                height: reviewTitle.height;
                anchors.top: parent.top;
                anchors.topMargin: 10;
                spacing: 10;

                Text {
                    id: reviewTitle;
                    width: parent.width;
                    text: title;
                    wrapMode: "WordWrap";
                }
            }

            Rectangle {
                width: parent.width;
                height: 1;
                border.color: "black";
            }

            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    Qt.openUrlExternally(guid);
                }
            }
        }
    }

    function showReviews(betaId) {
        var url = CommonScript.BETA_LABS_URL + "/node/" + betaId
            + "/forum/rss?type=review";
        betaReviewModel.source = url;
    }
}
